//
//  HMNewFeatureCollectionVC.swift
//  sinaweibo
//
//  Created by WGP on 16/4/11.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HMNewFeatureCollectionVC: UICollectionViewController {

/// 布局参数
    let layout = UICollectionViewFlowLayout()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(HMNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    init()
    {
        super.init(collectionViewLayout: layout)
        
//        设置Layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



    // MARK: UICollectionViewDataSource



    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return HMNewFeatureItemCount
        
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HMNewFeatureCell
    
//       设置cell的index,告诉他显示的是第几页
        cell.index = indexPath.item
    
        return cell
    }

}

extension HMNewFeatureCollectionVC
{
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = (Int)(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        
        if index == HMNewFeatureItemCount-1
        {
//            最后一页
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HMNewFeatureCell
            
            cell.startAnimation()
        }
    }
}


class HMNewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0
    {
        didSet
        {
            let imageName = "new_feature_\(index + 1)"
            bgImageView.image = UIImage(named: imageName)
          
            
//            隐藏按钮
            startBtn.hidden = true
        }
    }
    
    override  init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
//        添加子控件,注意cell的子控件都是添加到contentView
        contentView.addSubview(bgImageView)
        contentView.addSubview(startBtn)
        
//        添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
//    MARK: - 动画方法
    func startAnimation()
    {
//        显示按钮
        startBtn.hidden = false
        
//        将transform的缩放改为0
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                HMPrint("动画完成了")
                getApplicationDelegate.switchViewController(HMMainTabBarController())
        }
        
    }
    
//    MARK: - 懒加载
    private lazy var bgImageView: UIImageView  = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBtn: UIButton =
    {
        let button = UIButton()
        
//        设置
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        button.setTitle("开始体验", forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(HMNewFeatureBtnFontSize)
        
        button.sizeToFit()
        
        return button
    }()
}


















